
capture log close
log using $log/EDS_01_dataprep, text replace

clear
set mem 6000m
set matsize 800
set more off
set maxvar 5000
set trace off


		use $orig/IZA_ED_survey_mobi_search, clear

		
		gen search_dist50 = 1 if applied_distant==1 & search_radius>=50 & search_radius<9996
	    replace search_dist50 = 0 if search_dist50==.
		
	    replace search_radius=0 if search_radius==9996
		gen log_distance_km=log(search_radius+1)
		
		gen channel_active = 1 if (search_channel_2==1|search_channel_8==1|search_channel_9==1|search_channel_7==1)
		replace channel_active=0 if channel_active==.
		
		
		replace vacancy_referrals=0 if vacancy_referrals ==.
		replace vacancy_referrals = vacancy_referrals/dauloint_kw
		
		
		gen meetings_0bis2 = 1 if caseworker_meetings ==1
		replace meetings_0bis2 = 1 if caseworker_meetings ==5
		replace meetings_0bis2 = 0 if meetings_0bis2==.
	  
		gen meetings_3bis5 = 1 if caseworker_meetings ==2
		replace meetings_3bis5 = 0 if meetings_3bis5==.
	  
		gen meetings_6plus = 1 if caseworker_meetings ==3
		replace meetings_6plus = 1 if caseworker_meetings ==4
		replace meetings_6plus = 0 if meetings_6plus ==.
		
		gen meetings_3plus = (meetings_6plus==1|meetings_3bis5==1)
	  
		
		gen info_workfare=1 if ageny_offer_1 == 1
		replace info_workfare=0 if info_workfare==.
		
		gen info_jsc=1 if ageny_offer_2 == 1
		replace info_jsc=0 if info_jsc==.
	
		gen info_stt=1 if ageny_offer_3==1
		replace info_stt=0 if info_stt==.
		
		gen info_sus=1 if ageny_offer_5==1
		replace info_sus=0 if info_sus==.
		
	
	 #d;
	  global cov " _Isu_schule_0 _Isu_schule_1 _Isu_schule_2 _Isu_schule_3 
_Isu_ausbila0 _Isu_ausbila1 _Isu_ausbila2
_Isu_kat_al_1 _Isu_kat_al_2 _Isu_kat_al_3 _Isu_kat_al_4 
_Ichild_1 _Ichild_2
_Isu_eintri_0 _Isu_eintri_1 _Isu_eintri_2 _Isu_eintri_3 _Isu_eintri_4 _Isu_eintri_5
_Isu_eintri_6 _Isu_eintri_7 _Isu_eintri_8 _Isu_eintri_9 _Isu_eintri_10 _Isu_eintri_11
_Ibefstat_1 _Ibefstat_2 _Ibefstat_3 _Ibefstat_4
_Idauloint__7 dauloint_kw
_Idauloint__8 _Idauloint__9 _Idauloint__10 _Idauloint__11 _Idauloint__12 
_Idauloint__13 _Idauloint__14
		  female east_germany
          german migration_background 
         
          married  
		 benefit_status benefit_level log_benefit_level
     	  lifetime_ue_age lifetime_empl_age last_wage last_wage_mis 
          region_ba_aloq_zivall gdp_real_pc region_ba_vacancies 
	      sector_manufacturing sector_service  log_mobirate_avg5
	      openness_st conscient_st extraversion_st neuroticism_st loc_st
	      write_ger speak_ger write_eng speak_eng
	      homeowner   
	   ";
	   #d cr
	   
	   sum $cov
	   
	

		
save $data/data_individual,replace


		
use $orig/ba_bezirke,clear
set seed 12346753
reshape long close_rd_, i(region) j(nachbar)
drop if close_rd_==.

bys region: egen fweight_reg = max(nachbar)
gen test=1 if close_rd_<region
replace test=0 if test==.

gen region_help=1 if region<100
replace region_help=0 if region_help==.
gen close_help=1 if close_rd_<100
replace close_help=0 if close_help==.

tostring region close_rd_,replace

gen region2 =  substr(region,1,3) if region_help==0
replace region2 = "X" + substr(region,1,2) if region_help==1

gen close_rd2 = substr(close_rd_,1,3) if close_help==0
replace close_rd2 = "X" + substr(close_rd_,1,2) if close_help==1


gen region_pair=substr(region2,1,3) + substr(close_rd2,1,3) if test==1
replace region_pair=substr(close_rd2,1,3) +  substr(region2,1,3) if test==0
tab region_pair,g(county_pair_)

destring region , replace

joinby region using $data/data_individual, unmatched(master)
tab _merge
drop _merge

foreach type in applications_total applications_distant log_distance_km search_radius vacancy_referrals {
sum `type',d
	replace `type'=r(p99) if `type'>r(p99) & `type'!=.
		}
		gen applications_local = applications_total - applications_distant

save $data/data_pair,replace

use $orig/mobihilfen_all.dta, clear


gen ausgaben_uebergang = (uebergang*241)
gen ausgaben_ausruestung = (ausruestung*128)
gen ausgaben_fahrkosten = (fahrkosten*761) 
gen ausgaben_trennung = (trennung*881)
gen ausgaben_umzug = (umzug*1315)
gen ausgaben_reisekosten = (reisekosten*101) 



gen ausgaben_mp = (ausgaben_fahrkosten + ausgaben_trennung + ausgaben_umzug + ausgaben_reisekosten)/1000000

replace year = year + 1

keep ba year ausgaben_mp 
rename ba region
tempfile ausgaben
save `ausgaben'

use $data/data_pair, clear
rename ref_ue_y year
joinby region year using `ausgaben',unmatched(master)
tab _merge
drop _merge
label var ausgaben_mp "MP Expenditures in 1,000,000€"
save $data/data_pair, replace 

		
		
log close

